package com.kzj.laimeimei.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ab.util.AbSharedUtil;
import com.alibaba.fastjson.JSON;
import com.kzj.laimeimei.R;
import com.kzj.laimeimei.activity.GBaseActivity;
import com.kzj.laimeimei.beans.AdBean;
import com.kzj.laimeimei.beans.IndexServiceBean;
import com.kzj.laimeimei.global.App;
import com.kzj.laimeimei.global.Constant;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListAdapter;
import com.kzj.laimeimei.recyclerCard.basic.MaterialListView;
import com.kzj.laimeimei.recyclerCard.basic.SuperSwipeRefresh;
import com.kzj.laimeimei.recyclerCard.card.fragmentMain.MaterilistViewDirCard;
import com.kzj.laimeimei.recyclerCard.card.fragmentMain.ViewPagerCard;
import com.kzj.laimeimei.request.BaseResponse;
import com.kzj.laimeimei.request.ICallBackForRequestServer;
import com.kzj.laimeimei.request.RequestServerManager;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xyg on 2015/10/9.
 */
public class MainFragment extends BaseFragment implements SuperSwipeRefresh.OnRefreshReceyerListener {
    MaterialListView materialListView;
    SuperSwipeRefresh superSwipeRefresh;
    final int cardCount=6;
    ViewPagerCard viewPagerCard;
    List<AdBean> adBeanList;
    String adBeanListCache="adBeanListCache";
    String indexServiceBeansCache="indexServiceBeansCache";
    List<IndexServiceBean> indexServiceBeans;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View contentView = inflater.inflate(
                R.layout.template_superswiperefresh_materlistview,
                null);
      //  EventBus.getDefault().register(this);
        superSwipeRefresh = (SuperSwipeRefresh) contentView.findViewById(R.id.superSwipeRefresh);
        materialListView = (MaterialListView) contentView.findViewById(R.id.material_listview);
        superSwipeRefresh.setView(getActivity(), materialListView);
        superSwipeRefresh.setEnableLoadMore(false);
        superSwipeRefresh.setOnRefreshReceyerListener(this);
        fillArrayInList();

        TaskGetViewPager();
//		TaskGetAttentionListByType(AppConfig.getUser().getUserInfo().getUserId(), currentSuperType+"", swipeRefresh.getPage()+"", swipeRefresh.getPageSize()+"", true, false);
        return contentView;
    }

    @Override
    public void findViewByIds() {

    }

    @Override
    public void initViews() {

    }

    @Override
    public void fillArrayInList() {

        adBeanList=new ArrayList<>();
        String adBeanListString = AbSharedUtil.getString(App.getInstance().getApplicationContext(),adBeanListCache);
        if (!TextUtils.isEmpty(adBeanListString))
        {
            adBeanList=JSON.parseArray(adBeanListString, AdBean.class);
        }
        materialListView.clear();
        viewPagerCard=new ViewPagerCard(getActivity());
        viewPagerCard.setAdBeanList(adBeanList);
        MaterilistViewDirCard materilistViewCard =new MaterilistViewDirCard(getActivity());

        String indexServiceBeansString = AbSharedUtil.getString(App.getInstance().getApplicationContext(),indexServiceBeansCache);
        indexServiceBeans=new ArrayList<>();
        if (!TextUtils.isEmpty(indexServiceBeansString))
        {
            indexServiceBeans = JSON.parseArray(indexServiceBeansString, IndexServiceBean.class);
        }

        materilistViewCard.setIndexServiceBeanList(indexServiceBeans);
        materialListView.add(viewPagerCard);
        materialListView.add(materilistViewCard);

        materialListView.post(new Runnable() {
            @Override
            public void run() {
                materialListView.getAdapter().notifyDataSetChanged();
            }
        });


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
      //  EventBus.getDefault().unregister(getActivity());
    }

    @Override
    public void Refresh(MaterialListView recyclerView) {

        superSwipeRefresh.setRefreshing(false);
        TaskGetViewPager();
        TaskGetIndexService();

    }

    private void TaskGetViewPager() {
        RequestServerManager.getInstance().handleMethod(null, null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_ad_list, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_ad_list), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        adBeanList = JSON.parseArray(JSON.toJSONString(result.getRetval()), AdBean.class);
                        if (adBeanList != null && adBeanList.size() > 0) {
                            AbSharedUtil.putString(App.getInstance().getApplicationContext(), adBeanListCache, JSON.toJSONString(adBeanList));
                        }
                        viewPagerCard.getAdBeanList().clear();
                        viewPagerCard.getAdBeanList().addAll(adBeanList);
                        materialListView.getAdapter().notifyDataSetChanged();
                        TaskGetIndexService();
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }

    private void TaskGetIndexService() {
        RequestServerManager.getInstance().handleMethod((GBaseActivity) getActivity(), null, null, false,
                Constant.getRootUrl() + Constant.Action.Action_serviceList, RequestServerManager.getInstance().
                        getRequestParams(Constant.Action.Action_serviceList), BaseResponse.class, new ICallBackForRequestServer<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse result) {
                        indexServiceBeans = JSON.parseArray(JSON.toJSONString(result.getRetval()), IndexServiceBean.class);
                        if (indexServiceBeans != null) {
                            AbSharedUtil.putString(App.getInstance().getApplicationContext(), indexServiceBeansCache, JSON.toJSONString(indexServiceBeans));
                        }
                        MaterilistViewDirCard materilistViewCard = (MaterilistViewDirCard) ((MaterialListAdapter) (materialListView.getAdapter())).getCard(1);
                        materilistViewCard.getIndexServiceBeanList().clear();
                        materilistViewCard.getIndexServiceBeanList().addAll(indexServiceBeans);
                        materialListView.getAdapter().notifyDataSetChanged();
                    }

                    @Override
                    public void onFail(int type, String errorMsg, String json) {
                        App.getInstance().showToast(errorMsg + "");
                    }
                });
    }
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("MainActivity"); //统计页面
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("MainActivity");
    }
}
